package com.squareup.picasso;

import com.bytedance.common.utility.Logger;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public final class BitmapDiskLruCache {
    public DiskLruCache diskLruCache;
    private final Object b;

    public BitmapDiskLruCache(File file, long arg8) {
        this.b = new Object();
        Object v1 = this.b;
        synchronized (v1) {
            if (!file.exists()) {
                file.mkdirs();
            }

            if (Utils.c(file) > arg8) {
                try {
                    this.diskLruCache = DiskLruCache.open(file, arg8);
                } catch (IOException v0_1) {
                    Logger.e("BitmapDiskLruCache", "initDiskCache - " + v0_1);
                }
            }
        }
    }

    public final File getCacheFile(String arg9, InputStream arg10) {
        OutputStream v2_3;
        int v4;
        OutputStream v0_4 = null;
        DiskLruCache.Editor v2;
        DiskLruCache.Snapshot v0_3;
        File v1 = null;
        if (arg10 == null) {
            return v1;
        }
        String v3 = com.bytedance.common.utility.aa.md5(arg9);
        if (this.diskLruCache == null) {
            return v1;
        }

        try {
            v0_3 = this.diskLruCache.get(v3);
            if (v0_3 == null) {
                v2 = this.diskLruCache.edit(v3);
                if (v2 != null) {
                    v0_4 = v2.newOutputStream(0);
                    v4 = 4096;
                    byte[] v4_1 = new byte[v4];
                    while (true) {
                        int v5 = arg10.read(v4_1);
                        if (v5 == -1) {
                            break;
                        }
                        v0_4.write(v4_1, 0, v5);
                    }
                    v0_4.flush();
                    v2.commit();
                } else {
                }
            } else {
                v0_3.ins[0].close();
            }
            if (this.diskLruCache == null) {
            } else {
                v1 = this.diskLruCache.getCacheFile(v3);
            }
            return v1;
        } catch (IOException v0) {
            Logger.e("BitmapDiskLruCache", "getCacheFile - " + v0);
            return v1;
        } finally {
            try {
                v0_4.close();
            } catch (Exception e) {

            }
        }
    }

    public final File getCacheFile(String arg6) {
        File v0 = null;
        String v1 = com.bytedance.common.utility.aa.md5(arg6);
        if (this.diskLruCache != null) {
            try {
                v0 = this.diskLruCache.getCacheFile(v1);
            } catch (IOException v1_1) {
                Logger.e("BitmapDiskLruCache", "getCacheFile - " + v1_1);
            }
        }

        return v0;
    }

    public final InputStream b(String arg6) {
        InputStream v0 = null;
        String v1 = com.bytedance.common.utility.aa.md5(arg6);
        if (this.diskLruCache != null) {
            try {
                DiskLruCache.Snapshot v1_2 = this.diskLruCache.get(v1);
                if (v1_2 == null) {
                    return v0;
                }
                v0 = v1_2.ins[0];
            } catch (IOException v1_1) {
                Logger.e("BitmapDiskLruCache", "get - " + v1_1);
            }
        }

        return v0;
    }
}

